package com.taru.houseapp.until;

import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class FTPUtil {

    //ftp服务器IP地址
    private static final String FTP_ADDRESS= "59.110.44.156";
    //端口号
    private static final int FTP_PORT= 21;
    //用户名
    private static final String FTP_USERNAME="xiaokai";
    //密码
    private static final String FTP_PASSWORD="123456";
    //文件路径
    private static final String FTP_BASEPATH="/home/xiaokai/files";


    public static boolean uploadFile(List<String> fileNames, InputStream inputStream){
        boolean success= false;
        FTPClient ftp= new FTPClient();
        try {
            ftp.connect(FTP_ADDRESS);
            ftp.login(FTP_USERNAME,FTP_PASSWORD);
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftp.makeDirectory(FTP_BASEPATH);
            ftp.changeWorkingDirectory(FTP_BASEPATH);
            ftp.enterLocalPassiveMode();
            for(String fileName:fileNames){
                ftp.storeFile(fileName,inputStream);
            }
            inputStream.close();
            ftp.logout();
            success=true;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(ftp.isConnected()){
                try {
                    ftp.disconnect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }
}
